﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Gestion_de_Preguntas
{
    public partial class ListadoPreguntas: Form
    {
        
        private int user_id;

        public ListadoPreguntas(int user_id)
        {
            InitializeComponent();
            this.user_id = user_id;
        }

        private void ListadoPreguntas_Load(object sender, EventArgs e)
        {

        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();

            SqlConnection con = Program.obtenerConexion();

            SqlDataAdapter buscar = new SqlDataAdapter("SELECT preg_id as 'ID', preg_descripcion as 'Descripción'"
                + " FROM Monkey_Coders.Preguntas, Monkey_Coders.Publicaciones"
                + " WHERE preg_descripcion like '%" + textBoxDesc.Text + "%'"
                + " AND preg_publicacion_id = pub_cod"
                + " AND preg_respuesta_desc is NULL"
                + " AND pub_usuario_id = '" + user_id + "'", con);

            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(buscar);
            DataSet ds = new DataSet();
            buscar.Fill(ds);
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = ds.Tables[0];
            
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "Responder";
            btn.Name = "boton";
            btn.UseColumnTextForButtonValue = true;

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           DataGridViewRow fila;

            if (e.ColumnIndex > 0) 
                if (dataGridView1.Columns[e.ColumnIndex].Name == "boton")
                {
                    fila = dataGridView1.Rows[e.RowIndex];
                    Form responder = new Gestion_de_Preguntas.FormRespPreg(user_id,fila);
                    responder.Show();
                    dataGridView1.Columns.Clear();
                }
        }

    }
}
